package com.dong.aop;


import com.dong.config.T070AopConfig;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 测试aop   ,日志切面类
 *
 * JoinPoint  必须放到参数列表的第一个
 * @see T070AopConfig
 */
@Aspect
public class  LogAspect {
    public LogAspect() {
        System.out.println("logAcpect  create");
    }

   @Pointcut("execution(* *com.dong.aop.MathCalculator.*(..))")
    public void pointCut() {

    }

    /**
     *
     * @param joinPoint   获取拦截方法的相关信息
     */
    @Before(value = "pointCut()")
    public void logStart(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName()+"除法运行了");
        System.out.println(Arrays.asList(joinPoint.getArgs()));
    }
    @After(value = "pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println("除法结束了");
    }

    /**
     *
     * @param result  第一个参数用来接收返回值
     */
    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(JoinPoint joinPoint,Object  result) {
        System.out.println("除法正常返回 ,运行结果 {"+result+"}");
    }
    @AfterThrowing(value =  "pointCut()",throwing = "exception")
    public void logException( JoinPoint joinPoint,Exception exception) {
        System.out.println("除法异常....异常信息: {}"+joinPoint.getSignature().getName());
        exception.printStackTrace();
    }
   // @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around before ");
        try{
            Object proceed = pjp.proceed();
            System.out.println("around after ");
            System.out.println("around after return "+ proceed );
            return proceed;
        }catch (Exception e) {
            System.out.println("around after throw...");
        }
        return null;

    }
}
